﻿using MediatR;
using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Crypto;
using ProductManagement.Applcation.Command;
using ProductManagement.Domain.Dtos;
using ProductManagement.Domain.ProductsDomain;
using ProductManagement.Infrstructre.Interface;
using SqlSugar;
using System.Collections.Generic;

namespace ProductManagement.Applcation.CommandHanlers
{
    /// <summary>
    /// 产品显示中介者
    /// </summary>
    public class ProductSelectCommandHandler : IRequestHandler<ProductSelectCommand, ProductPage>
    {
        private readonly InterfaceProductRepository _productRepository;//产品仓储接口
        private readonly InterfaceProductsTypeRepository _productsTypeRepository;//产品类型仓储接口
        ISqlSugarClient db;

        public ProductSelectCommandHandler(InterfaceProductRepository productRepository, InterfaceProductsTypeRepository productsTypeRepository, ISqlSugarClient db)
        {
            _productRepository = productRepository;
            _productsTypeRepository = productsTypeRepository;
            this.db = db;
        }

        public async Task<ProductPage> Handle(ProductSelectCommand request, CancellationToken cancellationToken)
        {
            ProductPage page = new ProductPage();
            //var showproduct = (await _productRepository.GetAll()).Where(x => x.Office_Id == request.Office_Id && x.IsDel == false);
            //两表查
            var showproduct = db.Queryable<Product>()
                .InnerJoin<ProductType>((p, pt) => p.ProductTypeId == pt.ProductTypeId).Where(p => p.IsDel == false && p.Office_Id == request.Office_Id)
                .Select((p, pt) => new ProductSelectDto
                {
                    ProductId = p.ProductId,
                    PCharacteristic = p.PCharacteristic,
                    PNumber = p.PNumber,
                    PName = p.PName,
                    PSpec = p.PSpec,
                    PUnit = p.PUnit,
                    ProductTypeId = p.ProductTypeId,
                    PTClass = pt.PTClass,
                    IsDel = p.IsDel,
                });
            //产品名称
            if (!string.IsNullOrEmpty(request.PName))
            {
                showproduct = showproduct.Where(p => p.PName.Contains(request.PName));
            }
            //产品编号
            if (!string.IsNullOrEmpty(request.PNumber))
            {
                showproduct = showproduct.Where(p => p.PNumber == request.PNumber);
            }
            //分类
            if (request.ProductTypeId > 0)
            {
                //var typelist = (await _productsTypeRepository.GetAll()).Where(x => x.IsDel == false);
                //var res = BuildTree(typelist.ToList(), request.ProductTypeId);
                //var showlist = new List<ProductSelectDto>();
                //var showlist1 = new List<Product>();
                //foreach (var item in res)
                //{
                //    var newlist = (await _productRepository.GetAll()).Where(x => x.ProductTypeId == item.ProductTypeId).ToList();
                //    showlist1 = newlist;
                //    showlist.AddRange(showlist1);
                //}
                //page.SumCount = showproduct.Count();
                //page.PageLast = (int)Math.Ceiling(page.SumCount * 1.0 / request.PageSize);
                //page.list = showproduct.ToList().Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                //return page;
                showproduct = showproduct.Where(p => p.ProductTypeId == request.ProductTypeId);
            }
            //else
            //{
                page.SumCount = showproduct.Count();
                page.PageLast = (int)Math.Ceiling(page.SumCount * 1.0 / request.PageSize);
                page.list = showproduct.ToList().Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                return page;
            //}

        }
        //static List<ProductType> BuildTree(List<ProductType> list, int ids)
        //{
        //    List<ProductType> result = new List<ProductType>();
        //    var childs = list.Where(c => c.PFatherId == ids && c.IsDel == false).ToList();
        //    foreach (var item in childs)
        //    {
        //        result.Add(item);
        //        result.AddRange(BuildTree(list, item.ProductTypeId));
        //    }
        //    return result;
        //}
        //static async List<productTypesDto> BuildTree(List<ProductType> categories, int ?parentId)
        //{
        //    List<ProductType> result=new List<ProductType>();
        //    var childs = categories.Where(c => c.PFatherId == parentId && c.IsDel == false).ToList();
        //    foreach (var item in childs)
        //    {
        //        result.Add(item);
        //        result.AddRange(BuildTree(categories.ToList(), item.PFatherId));
        //    }
        //    return result;
        //    //return categories
        //    //    .Where(c => c.ProductTypeId == parentId && c.IsDel == false)
        //    //    .Select(c => new productTypesDto
        //    //    {
        //    //        id = c.ProductTypeId
        //    //    })
        //    //    .ToList();
        //}
        //public class productTypesDto
        //{
        //    public List<int> id { get; set; }
        //}

        ////查询当前以及父级的所有子类
        //static List<ProductType> BuildTree(List<ProductType> list, int ids)
        //{
        //    List<ProductType> result = new List<ProductType>();
        //    var childs = list.Where(c => c.PFatherId == ids && c.IsDel == false).ToList();
        //    foreach (var item in childs)
        //    {
        //        result.Add(item);
        //        result.AddRange(BuildTree(list, item.ProductTypeId));
        //    }
        //    return result;
        //}




    }
}
